<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="拓聊号" prop="cloudId">
        <el-input
          v-model="queryParams.cloudId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="bindCardId">
        <el-input
          v-model="queryParams.requestId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
  <el-form-item label="提现时间">
    <el-date-picker
        v-model="daterangeCreateTime"
        size="small"
        style="width: 240px"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
    ></el-date-picker>
  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['model:userWithdraw:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-tag type="danger">
          <span>总提现：</span>
          <span v-text="upayCount.count"></span>
        </el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag>
          <span>今日提现：</span>
          <span v-text="upayCount.today"></span>
        </el-tag>
      </el-col>
      <el-col :span="1.5">
        <el-tag type="info">
          <span>提现功能：</span>
          <el-switch
            v-model="withdrawSwitchStatus"
            active-value="1"
            inactive-value="0"
            @change="handleWithdrawSwitchChange"
          ></el-switch>
        </el-tag>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userWithdrawList" @selection-change="handleSelectionChange">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="拓聊号" align="center" prop="cloudId" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="headImg" >
        <template slot-scope="scope">
          <el-avatar shape="square" :src="scope.row.headImg" :style="randomRgb()" :size="30">
            {{scope.row.nickName}}
          </el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="银行卡号" align="center" prop="cardNo" />
      <el-table-column label="提现金额" align="center" prop="money" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="提现时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:userWithdraw:edit']"
          >审核通过</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:userWithdraw:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改upay提现对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <span v-else>确 认 审 核 通 过 ？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loadingForm" @click="submitForm">
          <span v-if="!loadingForm">确 认</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserWithdraw,selectCount, getUserWithdraw, delUserWithdraw, addUserWithdraw, updateUserWithdrawStatus, updateUserWithdraw, exportUserWithdraw } from "@/api/model/userWithdraw";
import {getConfigKey,updateConfigByKey} from "@/api/system/config";

export default {
  name: "UserWithdraw",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 提交防重复
      loadingForm:false,
      //提现开关
      withdrawSwitchStatus:null,
      baseUrl:process.env.VUE_APP_BASE_API,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // upay提现表格数据
      userWithdrawList: [],
      upayCount:{},
      configInfo:{},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // createTime时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'id',
        isAsc: 'desc',
        bindCardId: null,
        status: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("withdraw_status").then(response => {
      this.statusOptions = response.data;
      this.withdrawSwitch = response.data;
    });
    this.getCount();
    getConfigKey("withdraw_switch_status").then(res=>{
      const that = this;
      that.withdrawSwitchStatus = res.data;
    })
  },
  methods: {
    /** 查询upay提现列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && this.daterangeCreateTime.length>=2) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listUserWithdraw(this.queryParams).then(response => {
        this.userWithdrawList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCount(){
      selectCount().then(res=>{
        this.upayCount = res.data;
      });
    },
    handleWithdrawSwitchChange(){
      const that = this;
      let text = that.withdrawSwitchStatus === "0" ? "启用" : "停用";
      that.$confirm('确认要' + text + '提现功能吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        const data = {
          "configKey":"withdraw_switch_status",
          "configValue":that.withdrawSwitchStatus,
        }
        return updateConfigByKey(data);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function() {
        that.withdrawSwitchStatus = that.withdrawSwitchStatus === '1'?'0':'1';
      });
    },
    //随机头像背景颜色
    randomRgb(item) {
      let R = Math.floor(Math.random() * 130+110);
      let G = Math.floor(Math.random() * 130+110);
      let B = Math.floor(Math.random() * 130+110);
      return {
        'background-color': 'rgb(' + R + ',' + G + ',' + B + ')'
      };
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        cardId: null,
        bindCardId: null,
        amount: null,
        status: "0",
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加upay提现";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserWithdraw(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核提现";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loadingForm = true;
          if (this.form.id != undefined) {
            updateUserWithdrawStatus(this.form.id).then(response => {
              this.loadingForm = false;
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserWithdraw(this.form).then(response => {
              this.loadingForm = false;
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除upay提现编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserWithdraw(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有upay提现数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserWithdraw(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
